module.exports = function (app, models, $, db, nm) {
  //分类下商品列表
  app.get('/category/product',function(req,res){
    req.query.unitId = req.query.unitId || req.unitId;
    models.category_product.list(req.query,function(ret){
      res.send(ret);
    },{
      join:{
        productId:{
          type:'LEFT',
          name:'product',
          query:req.query.name ? { name:JSON.parse(req.query.name) } : null,
          projection:{
            name:1,
            imgs:1,
            description:1
          }
        }
      }
    })
  })
  //新增分类下商品
  app.post('/category/product',function(req,res){
    if(req.body.categoryId && req.body.productIds instanceof Array){
      var sqls = [];
      for(var i = 0; i < req.body.productIds.length; i ++){
        var query = { unitId: (req.body.unitId || req.unitId),categoryId:req.body.categoryId,productId:req.body.productIds[i] };
        var _remove = models.category_product.remove(query,null,{real:true});
        var _insert = models.category_product.insert(query);
        sqls.push(_remove + _insert);
      }
      db._eval(sqls.join(''),function(ret){
        res.send(ret);
      })
    }else{
      res.send({errorMsg:'参数错误'});
    }
  })
};
